<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>命名视图</title>
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>

<body>
  <div id="app">
    <router-view></router-view>
    <hr>
    <router-link to="/emails">Emails</router-link>
    <router-link to="/profile">Profile</router-link>
  </div>

  <script>
    // 1. 定义路由组件
    const Foo = {
      template: `
        <div>
          <h2>Foo</h2>
        </div>
      `
    }

    const UserSettings = {
      template: `
        <div>
          <h1>USER SETTINGS</h1>
          <foo></foo>
          <router-view></router-view>
          <router-view name="preview"></router-view>
        </div>
      `,
      components: {
        Foo
      }
    }

    const Emails = { template: `<div> Emails </div>` };

    const Profile = { template: `<div> Profile </div>` };
    const ProfilePreview = { template: `<div> ProfilePreview </div>` };

    // 2. 定义路由
    const routes = [
      {
        path: `/`,
        component: UserSettings,
        children: [
          {
            path: 'emails',
            component: Emails
          },
          {
            path: 'profile',
            components: {
              default: Profile,
              preview: ProfilePreview
            }
          }
        ]
      }
    ]

    // 3. 创建router实例
    const router = new VueRouter({
      routes
    })

    // 4. 创建vue实例
    const app = new Vue({
      router,
    }).$mount('#app');
  </script>
</body>

</html>